package com.example.gamestore.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.gamestore.Bean.Admin;
import com.example.gamestore.Bean.Game;
import com.example.gamestore.Bean.Orders;
import com.example.gamestore.Utils.JWTUtils;
import com.example.gamestore.aop.AdminLogAnnotation;
import com.example.gamestore.mapper.*;
import com.example.gamestore.service.AdminService;
import com.example.gamestore.service.GameService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import javax.servlet.http.HttpSession;
import javax.xml.soap.Node;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminControl {
    @Autowired
    private AdminService adminService;
    @Autowired
    private GameMapper gameMapper;
    @Autowired
    private GameService gameService;
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/admin")
    public String admin(){
        return "Admin/login";
    }
    @PostMapping("/loginadmin")
    public String login(Admin admin, Model model, HttpSession session){
        Admin USER=adminService.login(admin);
        if(USER!=null){
            session.setAttribute("admin",USER);
            return "redirect:/admin/index";
        }
        model.addAttribute("error","用户名或密码错误");
        return "Admin/login";
    }
    @GetMapping("admin/games")
    public String getgames(@RequestParam(name = "pn",defaultValue = "1")Integer pn,Model model){
        PageHelper.startPage(pn,6);
        List<Game> list = gameMapper.selectList(null);
        PageInfo<Game> info=new PageInfo<>(list,5);
        List<Game> list1 = info.getList();
        model.addAttribute("pages",info);
        return "Admin/table";
    }
    @PostMapping("/admin/updategames")
    @AdminLogAnnotation(module = "游戏模块",operater = "修改游戏")
    public String updategames(Game game,@RequestParam(name = "pn",defaultValue = "1") Integer pn){
        LambdaUpdateWrapper<Game> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(Game::getId,game.getId());
        gameMapper.update(game,wrapper);
        return "redirect:/admin/games?pn="+pn;
    }

    @PostMapping("/admin/getgame")
    @ResponseBody
    public Game getgames(Integer id){
        return gameService.selectGameByid(id);
    }

    @PostMapping("/admin/xiajia")
    @ResponseBody
    @AdminLogAnnotation(module = "游戏模块",operater = "上下架游戏")
    public String xiajia(@RequestParam(name = "gameid") Integer gameid){
        LambdaUpdateWrapper<Game> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(Game::getId,gameid);
        Game game = gameService.selectGameByid(gameid);
        Game updatestatus = updatestatus(game);
        Game gameupdate=new Game();
        gameupdate.setStatus(updatestatus.getStatus());
        gameMapper.update(gameupdate,wrapper);
        return "修改成功";
    }
    @GetMapping("/admin/index")
    public String index(Map<String,Object> map){
        indexmap(map);
        return "Admin/index";
    }

    /**
     * 修改游戏的状态
     * @param game
     * @return
     */
    public Game updatestatus(Game game){
        Integer status = game.getStatus();
        if(status==1){
            game.setStatus(0);
        }else {
            game.setStatus(1);
        }
        return game;
    }

    /**
     * 首页所需信息的展示
     * @param map
     */
    public void indexmap(Map<String,Object> map){
        Integer gamecount = gameMapper.selectCount(null);
        Integer contentcount = contentMapper.selectCount(null);
        Integer ordercount = orderMapper.selectCount(null);
        Integer usercount = userMapper.selectCount(null);
        Integer totalPrice = orderMapper.getTotalPrice();
        LambdaQueryWrapper<Game> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Game::getViewcount).last("limit 5");
        List<Game> list = gameMapper.selectList(wrapper);
        map.put("game",gamecount);
        map.put("content",contentcount);
        map.put("order",ordercount);
        map.put("user",usercount);
        map.put("price",totalPrice);
        map.put("gamelist",list);
    }

}
